﻿using System.Configuration;
using contSys.database.DAL;
using System.Data;

using contSys.Security;
using System;

namespace contSys.BL.Simulator
{
    public class MoveParams
    {
        private static string _connString = ConfigurationManager.ConnectionStrings["Simulator"].ConnectionString;
        private static string tableObjName = "MoveParams";

        public static string LastErrorMsg = string.Empty;

        #region get records
        public static DataSet GetByPlan(string usr, string pwd, object unitID, object planID)
        {
            if (UserManager.QueryAllowed(tableObjName, usr, pwd))
            {
                LastErrorMsg = string.Empty;
                string sql = string.Format("SELECT * FROM MoveParams WHERE (UnitID={0} AND PlanID={1})", unitID.ToString(), planID.ToString());
                return SQLiteDB.GetRecords(_connString, sql);
            }
            else
            {
                LastErrorMsg = "Failed security";
            }
            return null;
        }
        public static DataSet GetByMove(string usr, string pwd, object unitID, object moveID)
        {
            if (UserManager.QueryAllowed(tableObjName, usr, pwd))
            {
                LastErrorMsg = string.Empty;
                string sql = string.Format("SELECT * FROM MoveParams WHERE (UnitID={0} AND MoveID={1})", unitID.ToString(), moveID.ToString());
                return SQLiteDB.GetRecords(_connString, sql);
            }
            else
            {
                LastErrorMsg = "Failed security";
            }
            return null;
        }
        #endregion

        #region modify
        private static bool checkValidity(object UnitID, object afterMoveID, object secAfter)
        {
            int mi, sa, recCount;

            if (!int.TryParse(secAfter.ToString(), out sa) || sa < 0)
            {
                LastErrorMsg = "seconds must be >= 0"; 
                return false;
            }

            if (int.TryParse(afterMoveID.ToString(), out mi))
            {
                string sql = string.Format("SELECT MoveID FROM Moves WHERE (UnitID={0} AND MoveID={1})", UnitID, mi);
                if (SQLiteDB.TryCountRecords(_connString, sql, out recCount))
                {
                    if (recCount > 0) return true;
                }
            }

            LastErrorMsg = "After Move ID is invalid";
            return false;
        }
        public static bool Modify(string usr, string pwd, object unitID, object planID, object MoveID, object afterMoveID, object secAfter)
        {
            if (UserManager.ModifyAllowed(tableObjName, usr, pwd))
            {
                if (checkValidity(unitID, afterMoveID, secAfter))
                {
                    LastErrorMsg = string.Empty;
                    string sql = string.Format("UPDATE MoveParams SET afterMoveID={0}, secAfter={1} WHERE (UnitID={2} AND PlanID={3} AND MoveID={4})", afterMoveID.ToString(), secAfter.ToString(), unitID.ToString(), planID.ToString(), MoveID.ToString());
                    return SQLiteDB.TryExecuteScalar(_connString, sql);
                }
            }
            else
            {
                LastErrorMsg = "Failed security";
            }
            return false;
        }
        #endregion
    }
}
